package com.sky.apsect;

import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 公共字段自动填充切面类
 */
@Component
@Aspect
@Slf4j
public class AutoFillAspect {
	//233

    /**
     * 前置通知：增强新增和更新的业务方法
     */
    @Before("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.apsect.AutoFill)")
    public void autoFill(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        log.info("进入通知了......{}",name);

        //目标：补充基础属性：创建时间、创建人、更新时间、更新人
        //1.通过连接点对象，获取到方法的参数对象  eg: Employee
        Object[] args = joinPoint.getArgs();
        Object entity = null;
        if (args == null || args.length == 0) {//注意：判空的目的是为了防止空指针异常
            return;
        }
        entity = args[0];


        //2.判断操作类型，是更新还是新增
        //获取方法对象，目的获取方法上面的注解，从而拿到注解里面的属性值
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();   //方法签名
        AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);  //通过方法对象就就可以拿到注解
        OperationType value = autoFill.value(); //获取注解里的属性值


        if (value.equals(OperationType.INSERT)) {
            //如果是新增，则补充4个字段
            //3.通过反射，补充属性值
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setCreateTime.invoke(entity, LocalDateTime.now());
                setCreateUser.invoke(entity, BaseContext.getCurrentId());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            } catch (Exception e) {
                log.error("补充基础属性异常：{}",e.getMessage());
                throw new BaseException("公共字段补充失败！");
            }
        }else{
            //如果是更新，则补充2个字段
            //3.通过反射，补充属性值
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            } catch (Exception e) {
                log.error("补充基础属性异常：{}",e.getMessage());
                throw new BaseException("公共字段补充失败！");
            }
        }
    }
}
